﻿#include <vtkPolyData.h>
#include <vtkSTLReader.h>
#include <vtkSmartPointer.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkPlane.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkPlaneSource.h>
#include <vtkProp3D.h>
#include "vtkProperty.h"
#include <vtkImagePlaneWidget.h>

/// 如何装载stl文件，并作为PolyData显示出来

int main()
{
    std::string inputFilename = "D:/VTK/demo_cura.stl";

    vtkSmartPointer<vtkSTLReader> reader = vtkSmartPointer<vtkSTLReader>::New();
    reader->SetFileName(inputFilename.c_str());
    reader->Update();

    vtkSmartPointer<vtkPolyDataMapper> mapper =
            vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputConnection(reader->GetOutputPort());

    vtkSmartPointer<vtkActor> actor =
            vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);

    vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderer> rendererP = vtkSmartPointer<vtkRenderer>::New();

    vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->SetSize(800, 400);
    renderWindow->AddRenderer(renderer);
    renderWindow->AddRenderer(rendererP);
    renderer->SetViewport(0, 0, .5, 1);
    rendererP->SetViewport(0.5, 0, 1, 1);
//    pRender2->SetBackground(.3, .3, .3);
    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
            vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);

    renderer->AddActor(actor);
    renderer->SetBackground(.3, .6, .3);

    // 2.


    vtkSmartPointer<vtkImagePlaneWidget> planeWidget = vtkSmartPointer<vtkImagePlaneWidget>::New();

    planeWidget->SetInteractor(renderWindowInteractor);
    planeWidget->TextureVisibilityOn();

    double origin[] = {0, 1, 0};
    planeWidget->SetOrigin(origin);
    planeWidget->UpdatePlacement();
//    planeWidget->SetPicker(picker);
//    planeWidget->SetInputConnection(reader->GetOutputPort());
    //pWidget->SetResolution(10);//设置分辨率
//    planeWidget->RestrictPlaneToVolumeOn();
//    planeWidget->GetPlaneProperty()->SetColor(.9, .4, .4);
//    planeWidget->TextureInterpolateOn();
//    // planeWidget->SetWindowLevel(1358, -27);
////    planeWidget->SetDefaultRenderer(renderer);
//    planeWidget->DisplayTextOn();
//    planeWidget->SetResliceInterpolateToLinear();

//    planeWidget->SetPlaneOrientationToZAxes();
//    planeWidget->SetSliceIndex(14);

//    planeWidget->On();
//    planeWidget->InteractionOn();


//    vtkSmartPointer<vtkPlaneSource> plane = vtkSmartPointer<vtkPlaneSource>::New();

//    vtkSmartPointer<vtkPolyDataMapper> planeMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
//    planeMapper->SetInputConnection(plane->GetOutputPort());

//    vtkSmartPointer<vtkActor> planeActor = vtkSmartPointer<vtkActor>::New();
//    planeActor->SetMapper(planeMapper);
//    planeActor->SetTexture(planeWidget->GetTexture());

//    rendererP->AddActor(planeActor);

    renderWindow->Render();
//    renderWindowInteractor->Initialize();
    renderWindowInteractor->Start();
    return 0;
}
